﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using SoSlam.Common.Communication;
using SoSlam.Common.Communication.GameMessages;

namespace SoSlam.Common.ClientLogic
{
    public class PowerUp : DrawableGameComponent
    {
        public static readonly Dictionary<PowerupType, Color> PowerupColors = new Dictionary<PowerupType, Color>();

        private World _world;
        private TrackingCamera _camera;

        private AnimatedSprite _powerupSprite;

        private TimeSpan _scaleAnimationDuration = TimeSpan.FromMilliseconds(300);
        private TimeSpan _lastScaleChange = TimeSpan.Zero;

        public int PowerupId { get; set; }

        public bool IsHit { get; set; }

        public Vector2 Position { get; set; }

        public float Width { get { return _powerupSprite.Width; } }
        public float Height { get { return _powerupSprite.Height; } }

        public PowerupType Type { get; set; }

        public PowerUp(Game game, World world, TrackingCamera camera)
            : base(game)
        {
            _world = world;
            _camera = camera;
        }

        static PowerUp()
        {
            PowerupColors.Add(PowerupType.Gold, Color.FromNonPremultiplied(255, 255, 0, 255));
            PowerupColors.Add(PowerupType.Silver, Color.FromNonPremultiplied(200, 200, 200, 255));
            PowerupColors.Add(PowerupType.Power, Color.FromNonPremultiplied(158, 76, 0, 255));
        }

        public override void Initialize()
        {
            _powerupSprite = new AnimatedSprite(Game, new AnimationDefinition
            {
                AssetName = "Sprites/nut2",
                FrameRate = 10,
                FrameSize = new Point(Constants.POWERUP_WIDTH, Constants.POWERUP_HEIGHT),
                Loop = false,
                NbFrames = new Point(1, 1)
            });
            _powerupSprite.IsActive = false;

            _powerupSprite.Initialize();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _powerupSprite.LoadContent();

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            if (_powerupSprite.Scale <= 0)
                return;

            _powerupSprite.Update(gameTime);
            _powerupSprite.Position = this.Position - _camera.Position;

            if (IsHit
                && _lastScaleChange <= _scaleAnimationDuration)
            {
                _lastScaleChange = _lastScaleChange + gameTime.ElapsedGameTime;
                _powerupSprite.Scale = 1 - (float)Math.Max(_lastScaleChange.TotalSeconds / _scaleAnimationDuration.TotalSeconds, 0);
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            _powerupSprite.Color = PowerupColors[Type];
            _powerupSprite.Draw(gameTime, true);

            base.Draw(gameTime);
        }
    }
}
